package com.yimi.yinhepda.utils;

import android.os.Handler;
import android.os.Looper;

import com.yimi.yinhepda.db.dao.UcWaybillRegexTableDao;
import com.yimi.yinhepda.db.table.UcWaybillRegexTable;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lj
 * 优速运单号校验规则
 */
public class UcWaybillValidate {

    private static List<UcWaybillRegexTable> regexTableList;
    /**
     * 非主流程快递运单规则编码
     */
    private static String[] noWaybillType = new String[]{"HD", "DZHD", "15DZHD", "15HD", "WDCB", "CB", "ZXCB", "SFS",
            "HBJZD", "HBJB", "JZBQ", "JZBQ2", "WZJ"};
    /**
     * 快递子单规则编码
     */
    private static String[] childWaybillType = new String[]{"CDZMD", "CYS", "CZZMD"};

    static List<Pattern> patterns = new ArrayList<>();

//    static {
//        patterns.add(Pattern.compile("8894[34][0-9]{6}"));
//        patterns.add(Pattern.compile("[8][8][9][8][5][0-9]{7}"));
//        patterns.add(Pattern.compile("[8][8][9][5][1][8][8][1-9][0-9]{7}"));
//        patterns.add(Pattern.compile("[8][8][9][3][1][9][0-9]{9}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][1][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][2][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][3][0-9]{8}"));
//        patterns.add(Pattern.compile("889310[012][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][3][2][9][0][0-9]{8}"));
//        patterns.add(Pattern.compile("8893200[0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][4][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][0][0-9]{8}"));
//        patterns.add(Pattern.compile("8890100[0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][6][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][5][1][8][7][0-9]{8}|[8][8][9][5][1][8][8][0][0-9]{7}"));
//        patterns.add(Pattern.compile("[8][8][9][8][5][0-9]{10}"));
//        patterns.add(Pattern.compile("[8][8][9][9][0][0][7][0-9]{8}"));
//        patterns.add(Pattern.compile("889518[0-6][0-9]{8}"));
//        patterns.add(Pattern.compile("[5][1][8][8][1-9][0-9]{7}"));
//        patterns.add(Pattern.compile("[3][6][0-9]{10}"));
//        patterns.add(Pattern.compile("[3][1][9][0-9]{9}"));
//        patterns.add(Pattern.compile("[9][0][0][1][0-9]{8}"));
//        patterns.add(Pattern.compile("[9][0][0][2][0-9]{8}"));
//        patterns.add(Pattern.compile("[9][0][0][3][0-9]{8}"));
//        patterns.add(Pattern.compile("310[012][0-9]{8}"));
//        patterns.add(Pattern.compile("[3][2][9][0][0-9]{8}"));
//        patterns.add(Pattern.compile("WD[0-9]{8}"));
//        patterns.add(Pattern.compile("[G][0-9]{8}"));
//        patterns.add(Pattern.compile("3200[0-9]{8}"));
//        patterns.add(Pattern.compile("[9][0][0][4][0-9]{8}"));
//        patterns.add(Pattern.compile("[3][3][0-9]{10}"));
//        patterns.add(Pattern.compile("C[0-9]{7}"));
//        patterns.add(Pattern.compile("[9][0][0][0][0-9]{8}"));
//        patterns.add(Pattern.compile("0100[0-9]{8}"));
//        patterns.add(Pattern.compile("[9][0][0][6][0-9]{8}"));
//        patterns.add(Pattern.compile("[3][4][0-9]{10}"));
//        patterns.add(Pattern.compile("[5][1][8][7][0-9]{8}|[5][1][8][8][0][0-9]{7}"));
//        patterns.add(Pattern.compile("[2][0][0-9]{11}"));
//        patterns.add(Pattern.compile("[W][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][5][0-9]{7}"));
//        patterns.add(Pattern.compile("[8][5][0-9]{10}"));
//        patterns.add(Pattern.compile("4[34][0-9]{6}"));
//        patterns.add(Pattern.compile("[9][0][0][7][0-9]{8}"));
//        patterns.add(Pattern.compile("[2][0][0-9]{12}"));
//        patterns.add(Pattern.compile("518[0-6][0-9]{8}"));
//        patterns.add(Pattern.compile("[8][8][9][0][0-9]{11}"));
//    }

    public static boolean validate(String w) {
        if (regexTableList == null || regexTableList.isEmpty()) {
            regexTableList = new UcWaybillRegexTableDao().findAll();
        }

        if (patterns.isEmpty()) {
            if (!regexTableList.isEmpty()) {
                for (UcWaybillRegexTable item : regexTableList) {
                    patterns.add(Pattern.compile(item.getFldS2()));
                }
            } else {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.showToast("未获取到快递运单规则！");
                    }
                });

                return false;
            }
        }

        if (!patterns.isEmpty()) {
            for (Pattern pattern : patterns) {
                Matcher matcher = pattern.matcher(w);
                while (matcher.matches()) {
                    return true;
                }
            }
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showToast("未获取到快递运单规则！");
                }
            });
            return false;
        }

        return false;
    }

    /**
     * 判断是否快递子单
     *
     * @param waybill
     * @return
     */
    public static boolean isUcChildWyabill(String waybill) {
        if (regexTableList == null || regexTableList.isEmpty()) {
            regexTableList = new UcWaybillRegexTableDao().findAll();
        }
        if (!regexTableList.isEmpty()) {
            for (UcWaybillRegexTable item : regexTableList) {
                if (childWaybillTypeFunc(item.getTypeCode())) {
                    if (Pattern.compile(item.getFldS2()).matcher(waybill).matches()) {
                        return true;
                    }
                }
            }
        } else {
            ToastUtil.showToast("未获取到快递运单规则！");
            return false;
        }
        return false;
    }

    /**
     * 非主流程快递运单规则编码匹配
     *
     * @param typeCode 规则编码
     * @return true-非主流程，false-主流程
     */
    private static boolean noWaybillTypeFunc(String typeCode) {
        for (String item : noWaybillType) {
            if (item.equals(typeCode)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 快递子单规则编码匹配
     *
     * @param typeCode 规则编码
     * @return true-是快递子单,false-非快递子单
     */
    private static boolean childWaybillTypeFunc(String typeCode) {
        for (String item : childWaybillType) {
            if (item.equals(typeCode)) {
                return true;
            }
        }
        return false;
    }

//    public static boolean validate(String w) {
//        for (Pattern pattern : patterns) {
//            Matcher matcher = pattern.matcher(w);
//            while (matcher.matches()) {
//                return true;
//            }
//        }
//        return false;
//    }

    public static void main(String[] args) throws Exception {
        System.out.println(validate("900355800855"));
    }

    public static void clearData(){
        if (regexTableList!=null&&regexTableList.size()>0){
            regexTableList.clear();
        }
    }
}
